package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UserPeer extends BasePeer{
	public static final String tableName = "User";
	public static final String USERID = "userId";
	public static final String USERNAME = "username";
	public static final String PRIMARYEMAIL = "primaryEmail";
	public static final String PASSWORD = "password";
	public static final String STATUS = "status";
	public static final String STATE = "state";
	public static final String ONLINESTATE = "onlineState";
	public static final String LOGINTOKEN = "loginToken";
	public static final String TOKENEXPIRED = "tokenExpired";
	public static final String REGISTERTIME = "registerTime";
	public static final String LASTACTIVETIME = "lastActiveTime";
	public static final String LASTLOGINTIME = "lastLoginTime";
	public static final String LASTLOGOUTTIME = "lastLogoutTime";
	public static final String TOTALONLINEMINUTES = "totalOnlineMinutes";
	public static final String[] columnNames=new String[]{"userId","username","primaryEmail","password","status","state","onlineState","loginToken","tokenExpired","registerTime","lastActiveTime","lastLoginTime","lastLogoutTime","totalOnlineMinutes"};
	public static User getUserByPKs(Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserPeer.tableName,UserPeer.columnNames," TRUE  AND `userId`=? ",new Object[]{userId,},0, 1,conn);
			User user = new User();
		fill(rs,user);
		return user;
	}
	public static User getUserReferenceColumnsOnlyByPKs(Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserPeer.tableName,new String[]{"userId",}," TRUE AND `userId`=?",new Object[]{userId,}, 0,  1,conn);
			User user = new User();
		fill(rs,user);
		return user;
	}
	public static User getUserByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserPeer.tableName,UserPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			User user = new User();
		fill(rs,user);
		return user;
	}
	public static User getUserByUsername(java.lang.String username, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserPeer.tableName,UserPeer.columnNames,"`username`=?",new Object[]{username},0, 1,conn);
			User user = new User();
		fill(rs,user);
		return user;
	}
	public static User getUserByPrimaryEmail(java.lang.String primaryEmail, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserPeer.tableName,UserPeer.columnNames,"`primaryEmail`=?",new Object[]{primaryEmail},0, 1,conn);
			User user = new User();
		fill(rs,user);
		return user;
	}
	public static User getUserByLoginToken(java.lang.String loginToken, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserPeer.tableName,UserPeer.columnNames,"`loginToken`=?",new Object[]{loginToken},0, 1,conn);
			User user = new User();
		fill(rs,user);
		return user;
	}
	public static void fill(ResultSet rs, User user) throws SQLException {
		if(rs.next()){
			try{user.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{user.setUsername(rs.getString("username"));}catch(Exception ex){}
			try{user.setPrimaryEmail(rs.getString("primaryEmail"));}catch(Exception ex){}
			try{user.setPassword(rs.getString("password"));}catch(Exception ex){}
			try{user.setStatus(rs.getInt("status"));}catch(Exception ex){}
			try{user.setState(rs.getInt("state"));}catch(Exception ex){}
			try{user.setOnlineState(rs.getInt("onlineState"));}catch(Exception ex){}
			try{user.setLoginToken(rs.getString("loginToken"));}catch(Exception ex){}
			try{user.setTokenExpired(rs.getBoolean("tokenExpired"));}catch(Exception ex){}
			try{user.setRegisterTime(rs.getTimestamp("registerTime"));}catch(Exception ex){}
			try{user.setLastActiveTime(rs.getTimestamp("lastActiveTime"));}catch(Exception ex){}
			try{user.setLastLoginTime(rs.getTimestamp("lastLoginTime"));}catch(Exception ex){}
			try{user.setLastLogoutTime(rs.getTimestamp("lastLogoutTime"));}catch(Exception ex){}
			try{user.setTotalOnlineMinutes(rs.getInt("totalOnlineMinutes"));}catch(Exception ex){}
			user.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<User> userCollection) throws SQLException {
		while(rs.next()){
			User user=new User();
			try{user.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{user.setUsername(rs.getString("username"));}catch(Exception ex){}
			try{user.setPrimaryEmail(rs.getString("primaryEmail"));}catch(Exception ex){}
			try{user.setPassword(rs.getString("password"));}catch(Exception ex){}
			try{user.setStatus(rs.getInt("status"));}catch(Exception ex){}
			try{user.setState(rs.getInt("state"));}catch(Exception ex){}
			try{user.setOnlineState(rs.getInt("onlineState"));}catch(Exception ex){}
			try{user.setLoginToken(rs.getString("loginToken"));}catch(Exception ex){}
			try{user.setTokenExpired(rs.getBoolean("tokenExpired"));}catch(Exception ex){}
			try{user.setRegisterTime(rs.getTimestamp("registerTime"));}catch(Exception ex){}
			try{user.setLastActiveTime(rs.getTimestamp("lastActiveTime"));}catch(Exception ex){}
			try{user.setLastLoginTime(rs.getTimestamp("lastLoginTime"));}catch(Exception ex){}
			try{user.setLastLogoutTime(rs.getTimestamp("lastLogoutTime"));}catch(Exception ex){}
			try{user.setTotalOnlineMinutes(rs.getInt("totalOnlineMinutes"));}catch(Exception ex){}
			user.resetModified();
			userCollection.add(user);
		}
	}
	public static boolean update(User user, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UserPeer.columnNames.length; i++) {
			if (((UserBase)user).modified[i]) {
				columns.add(UserPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `User` SET ");
		sql.append("WHERE TRUE AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,user.get(column));
		}
		pstmt.setObject(columns.size()+1+0,user.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(User user, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (user.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `User` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (user.get(column) != null) {
				pstmt.setObject(i + 1 + j, user.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<User> getUserCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<User> collection=new Vector<User>();
		fill(rs,collection);
		return collection;
	}
}
